﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EmoneyWeiXinDAL;
using EmoneyWeiXinEntity;

namespace EmoneyWeiXinBLL
{
    public class UserBLL
    {
        public List<UserEntity> getUserAll(string username, string userpwd)
        {
            UserDAL dal = new UserDAL();
            return dal.getUserAll(username, userpwd);
        }

        public List<UserEntity> getUserList(int pindex, int psize, out int total)
        {
            UserDAL dal = new UserDAL();
            return dal.getUserList(pindex, psize, out  total);
        }
        public UserEntity getUserListByID(int id)
        {
            UserDAL dal = new UserDAL();
            var list= dal.getUserListByID(id);
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
               return new UserEntity();
            }
        }
        public int UpdateUser(UserEntity obj)
        {
            UserDAL dal = new UserDAL();
            return dal.UpdateUser(obj);
        }
        public int UpdateUser2(UserEntity obj)
        {
            try
            {
                UserDAL dal = new UserDAL();
                var ir = dal.UpdateUser(obj);
                if (ir > 0)
                {
                    if (!string.IsNullOrEmpty(obj.leftmenuids))
                    {
                        for (int i = 0; i < obj.leftmenuids.Split(',').Length; i++)
                        {
                            var leftmemu_id = int.Parse(obj.leftmenuids.Split(',')[i]);
                            if (!string.IsNullOrEmpty(obj.operates))
                            {
                                if (i < obj.operates.Split(';').Length)
                                {
                                    var operate_id = obj.operates.Split(';')[i];
                                    UpdateLeftmemu_operate(1, ir, leftmemu_id, operate_id);
                                }
                            }
                        }
                    }
                }
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        public int UpdateLeftmemu_operate(int op, int userid, int leftmemu_id, string operate_id)
        {
            UserDAL dal = new UserDAL();
            return dal.UpdateLeftmemu_operate(op, userid, leftmemu_id, operate_id);
        }
        public int DeleteUser(int id)
        {
            UserDAL dal = new UserDAL();
            return dal.DeleteUser(id);
        }

        public UserEntity GetUserEntityByTxzId(int txzId) 
        {
            UserDAL dal = new UserDAL();
            return dal.GetUserEntityByTxzId(txzId);
        }
    }
}
